package chapter1;

import java.util.Iterator;

public class LinkedBag<T> implements Bag<T> {
    private int index = 0;
    private Node<T> head = null;

    @Override
    public void add(T item) {
        if (item == null) {
            new NullPointerException("item cannot be null");
        }
        Node oldHead = head;
        head = new Node<>();
        head.setItem(item);
        head.setNext(oldHead);
        index++;
    }

    @Override
    public boolean isEmpty() {
        return index == 0;
    }

    @Override
    public int size() {
        return index;
    }

    @Override
    public Iterator<T> iterator() {
        return new LinkedIterator<>(head);
    }




}
